/*! \page license
 *
 * Copyright (c) 2011, Blaise Lengrand
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * Neither the name of `snWin' nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

var test_feed_date_current = new Array();
var test_feed_date_previous = new Array();
var test_feed_i = 1;

const test_feed = {
  'display_name': "Test Feed",
  'author': "Blaise Lengrand",
  'id': "test_feed",
  'default_color': "red",
  'ep': {
    'connect': test_feed_connect,
    'is_connected': test_feed_is_connected,
    'update': test_feed_update,
    'previous': test_feed_previous,
    'refresh': test_feed_refresh,
    'comment': test_feed_post_comment,
    'publish': test_feed_publish,
    'share': test_feed_share,
    'like': test_feed_like,
    'add_fields': test_feed_add_fields
  },
  // Extra parameters with there default values (will be saved in the session)
  'session': {
    'new_feed_rate': 5
  },
  'update_rate_s': 10,
};

//! Add the feed to the list
feed_available[test_feed.id] = test_feed;

function test_feed_is_connected(feed_id)
{
  return true;
}

function test_feed_connect(feed_id, context)
{
  const color = ['red', 'yellow', 'blue', 'green'];
  var display_name = test_feed.display_name + " " + test_feed_i;
  var displayed_color = color[test_feed_i % color.length];

  if (!context || !context.display_name)
    context.display_name = display_name;
  if (!context || !context.color)
    context.color = displayed_color;

  setTimeout("test_feed_connect_callback(\"" + feed_id + "\");", test_feed_random(1000, 5000));
  feed_register(feed_id, test_feed, context);

  feed_log("Connecting", feed_id);

  test_feed_i++;
}

function test_feed_connect_callback(feed_id)
{
  feed_log("Connected", feed_id);

  // Initialize the variables
  test_feed_date_current[feed_id] = feed_current_time();
  test_feed_date_previous[feed_id] = test_feed_date_current[feed_id];
  // Feedback
  feed_connected(feed_id);
}

function test_feed_previous(feed_id)
{
  feed_log("Get previous feeds", feed_id);

  // Check if this is the last entry
  var end = (test_feed_random(0, 20) == 0);

  for (var i=0; i<test_feed_random(1, 5); i++)
  {
    test_feed_date_previous[feed_id] -= test_feed_random(60, 3600);
    test_feed_create_feed(test_feed_date_previous[feed_id], feed_id);
  }

  // Tell the core module that the feed has been acquired
  feed_callback(feed_id);
}

function test_feed_update(feed_id)
{
  feed_log("Get new feeds", feed_id);

  if (!test_feed_random(0, feed[feed_id].session.new_feed_rate))
  {
    test_feed_date_current[feed_id] = feed_current_time();
    test_feed_create_feed(test_feed_date_current[feed_id], feed_id);
  }

  // Tell the core module that the feed has been acquired
  feed_callback(feed_id);
}

function test_feed_refresh(feed_id, entry)
{
  feed_callback(feed_id);
}

function test_feed_random(min, max)
{
  return Math.floor(Math.random() * (max - min)) + min;
}

function test_feed_gen_sentence(length, extra)
{
  var base = "aaaabcdeeeeefghiiijklmnooopqrstuuvwxyz         " + extra;
  var text = "";

  for (var i=0; i<length; i++)
    text += base.charAt(test_feed_random(0, base.length));

  return text;
}

function test_feed_gen_link()
{
  var base = "aaaabcdeeeeefghiiijklmnooopqrstuuvwxyz1234567890";
  var text = "http://";

  for (var j=0; j<test_feed_random(2, 10); j++)
  {
    for (var i=0; i<test_feed_random(5, 15); i++)
      text += base.charAt(test_feed_random(0, base.length));
    text += "/"
  }
  return text;
}

function test_feed_create_feed(date, feed_id)
{
  // Extra
  var extra = new Array();
  var name = test_feed_gen_sentence(test_feed_random(5, 32));
  if (test_feed_random(0, 2))
    extra["to"] = test_feed_gen_sentence(test_feed_random(5, 32));
  var picture = "http://b.static.ak.fbcdn.net/rsrc.php/yo/r/UlIqmHJn-SK.gif";
  var message = test_feed_gen_sentence(test_feed_random(10, 100), "\n");
  if (test_feed_random(0, 2))
    message += " " + test_feed_gen_link() + " ";
  message += test_feed_gen_sentence(test_feed_random(10, 100), "\n");
  var link = test_feed_gen_link();
  var icon = (test_feed_random(0, 2))?"http://b.static.ak.fbcdn.net/rsrc.php/yz/r/StEh3RhPvjk.gif":"";

  feed_log("Creating new feed entry", feed_id);

  // --- date_after_text
  if (test_feed_random(0, 2))
    extra["date_after_text"] = {'text': " via " + test_feed_gen_sentence(test_feed_random(4, 20)) };
  // --- comment
  if (test_feed_random(0, 2))
  {
    var data = new Array();
    var nb = test_feed_random(0, 20);
    var comment_date = date;
    for (var i=0; i<test_feed_random(0, nb); i++)
    {
      comment_date += test_feed_random(60, 1000);
      data[i] = {
        'name': test_feed_gen_sentence(test_feed_random(4, 32)),
        'text': test_feed_gen_sentence(test_feed_random(4, 100)),
        'date': comment_date
      }
    }
    extra["comment"] = {
      'nb': nb,
      'link': test_feed_gen_link(),
      'data': data
    };
  }
  if (test_feed_random(0, 2))
  {
    extra["like"] = {
      'nb': test_feed_random(0, 6),
      'link': test_feed_gen_link()
    };
  }

  // Add a fake entry
  feed_entry_add({
    'name': name,
    'picture': picture,
    'message': message,
    'link': link,
    'date': date,
    'icon': icon,
    'extra': extra
  }, feed_id);
}

function test_feed_post_callback(feed_id)
{
  feed_callback(feed_id);
}

function test_feed_post_comment(feed_id, entry, text)
{
  feed_log("Comment, Entry ID: '" + entry.id + "', Message: '" + text + "'", feed_id);
  setTimeout("test_feed_post_callback('" + entry.feed_id + "')", test_feed_random(0, 10) * 1000);
}

function test_feed_publish(feed_id, text)
{
  setTimeout(function() {
    feed_entry_add({
      'name': "Me",
      'message': text,
      'date': feed_current_time(),
    }, feed_id);
    feed_callback(feed_id);
  }, test_feed_random(0, 10) * 1000);
}

function test_feed_share(feed_id, entry, share_info)
{
  setTimeout(function() {
    feed_entry_add({
      'name': share_info.title,
      'picture': share_info.picture,
      'message': share_info.message,
      'link': share_info.link,
      'date': feed_current_time()
    }, feed_id);
    feed_callback(feed_id);
  }, test_feed_random(0, 10) * 1000);
}

function test_feed_like(feed_id, entry)
{
  feed_log("Like, Entry ID: '" + entry.id + "'", feed_id, "alert");
  setTimeout("test_feed_post_callback('" + feed_id + "')", test_feed_random(0, 10) * 1000);
}

function test_feed_add_fields()
{
  var div_container = document.createElement("div");
  var html = "New feed rate: <input name=\"new_feed_rate\" type=\"text\" /> <span class=\"comment\">(the bigger, the slower)</span>";
  div_container.innerHTML = html;
  return div_container;
}
